package com.youhome.service.service.impl;


import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.BCounselorMapper;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.BUser;
import com.youhome.service.service.BUserService;
import com.youhome.service.service.CounselorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by chen365 on 2018/09/17.
 */
@Service
public class CounselorServiceImpl implements CounselorService {
    @Autowired
    private BCounselorMapper bCounselorMapper;
    @Autowired
    private BUserService userService;


    @Override
    public int deleteByPrimaryKey(Long counselorId) {
        return bCounselorMapper.deleteByPrimaryKey(counselorId);
    }

    @Override
    public int insert(BCounselor record) {
        return bCounselorMapper.insert(record);
    }

    @Override
    public int insertSelective(BCounselor record) {
        return bCounselorMapper.insertSelective(record);
    }

    @Override
    public BCounselor selectByPrimaryKey(Long counselorId) {
        return bCounselorMapper.selectByPrimaryKey(counselorId);
    }
/*
    @Override
    public BCounselor selectByToken(String token) {
        String usernameFromToken = new JwtTokenUtil().getUsernameFromToken(token);
        System.err.println("detail-usernameFromToken:" + usernameFromToken);
        BUser bUser = userService.selectByPhone(usernameFromToken);
        if (bUser != null) {
            BCounselor bCounselor = selectByUserId(bUser.getUserId());
            return bCounselor;
        }
        return null;
    }

    @Override
    public BCounselor selectByToken(HttpServletRequest request) {
        BCounselor bCounselor = null;
        String token = request.getHeader("token");
        System.err.println("detail-token:" + token);
        String usernameFromToken = new JwtTokenUtil().getUsernameFromToken(token);
        System.err.println("detail-usernameFromToken:" + usernameFromToken);
        BUser bUser = userService.selectByPhone(usernameFromToken);
        if (bUser != null) {
            bCounselor = selectByUserId(bUser.getUserId());
        }
        return bCounselor;
    }

    */

    @Override
    public BCounselor selectByUserId(Long userId) {
        return bCounselorMapper.selectByUserId(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(BCounselor record) {
        record.setUtime(new Date());
        return bCounselorMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BCounselor record) {
        record.setUtime(new Date());
        return bCounselorMapper.updateByPrimaryKey(record);
    }

    @Override
    public BCounselor selectByPhone(String phone) throws CustomException {
        BUser bUser = userService.selectByPhone(phone);
        if(bUser != null) {
            return bCounselorMapper.selectByUserId(bUser.getUserId());
        }
        throw  new CustomException(ResultCode.FAIL);
    }
}
